[Coding045] LeetCode 316

Remove Duplicate Letters

Cook 2024.04.13

More coding records

Get the knowledge flowing and circulating! :)

目录

本题收获

题目:316. Remove Duplicate Letters

Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

image-20240412115749957

Example 1:

Example 2:

Constraints:

Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/


代码1(配 · 手绘过程图)

image-20240414113554744

Fig. 正常逻辑

image-20240414112720359

Fig. 未能考虑充分的情况

代码解读 | 评价

  • 这个特殊情况的解读在代码2的line26~line35

####


代码2

代码解读 | 评价

  • 第一版代码错误的原因就是“a b a c b”这个测试用例没有分析仔细。具体的分析过程如代码line24所示。

    • 特别注意,continue的含义!